package br.com.onlinebidding.facade;

import java.util.List;

import br.com.onlinebidding.dao.ProductDAO;
import br.com.onlinebidding.model.Product;

public class ProductFacade {

	private ProductDAO productDAO = new ProductDAO();

	public void insertProduct(Product product) {
		this.productDAO.beginTransaction();
		this.productDAO.save(product);
		this.productDAO.commitAndCloseTransaction();
	}

	public void updateProduct(Product product) {
		this.productDAO.beginTransaction();
		Product persistedProduct = this.productDAO.find(product.getId());
		persistedProduct.setName(product.getName());
		persistedProduct.setDescription(product.getDescription());
		this.productDAO.update(persistedProduct);
		this.productDAO.commitAndCloseTransaction();
	}

	public void updateInUse(int id) {
		this.productDAO.beginTransaction();
		Product persistedProduct = this.productDAO.find(id);
		persistedProduct.setInUse(true);
		this.productDAO.update(persistedProduct);
		this.productDAO.commitAndCloseTransaction();
	}

	public Product findProduct(int productId) {
		this.productDAO.beginTransaction();
		Product product = this.productDAO.find(productId);
		this.productDAO.closeTransaction();
		return product;
	}

	public List<Product> listAll() {
		this.productDAO.beginTransaction();
		List<Product> result = this.productDAO.findAll();
		this.productDAO.closeTransaction();
		return result;
	}
	
	public List<Product> listAllProductsNotUse() {
		this.productDAO.beginTransaction();
		List<Product> result = this.productDAO.findAllProductsNotUse();
		this.productDAO.closeTransaction();
		return result;
	}


	
	public void deleteProduct(Product product) {
		this.productDAO.beginTransaction();
		Product persistedProduct = this.productDAO.find(product.getId());
		this.productDAO.delete(persistedProduct);
		this.productDAO.commitAndCloseTransaction();
	}
}
